import multi_robot_problem as robprob
from random import randint
from sys import maxint
#import graphic_problems_1
#import graphic_problems_2  

p1 = robprob.MultiRobotState(7,9,                       #dimesions
                           ((2,2),),                    #robots
                           ((1,1),(3,1),(5,1)),         #dirt
                           ()                           #obstecales
                            )

p2 = robprob.MultiRobotState(9,9,                       #dimesions
                           ((1,1),(6,8)),               #robots
                           ((1,0),(0,1),(2,2),(8,8)),   #dirt
                           ((5,5),)                     #obstecales
                            )

p3 = robprob.MultiRobotState(9,9,                       #dimesions
                           ((1,1),(6,8)),               #robots
                           ((1,0),(0,1),(2,2),(8,8)),   #dirt
                           ((5,5),(7,3),(3,7))          #obstecales
                            )

p4 = robprob.MultiRobotState(10,12,                       #dimesions
                           ((1,1),(6,8),(4,3)),               #robots
                           ((1,0),(0,1),(2,2),(8,8),(9,11),(0,6)),   #dirt
                           ((5,5),(7,3),(1,5),(2,6),(3,7),(4,8),(5,9),(6,10),(0,5))          #obstecales
                            )


def random_problem(mini=0,max=maxint, n_robs=maxint, n_dirt=maxint, n_obs=maxint):
    x = randint(mini,max)
    y = randint(mini,max)
    def rand_xy():
        return (randint(0,x-1),randint(0,y-1))
    robs = []
    if n_robs == maxint: num_robots = randint(0,x*y)
    else: num_robots = min(x*y,n_robs)
    for i in range(num_robots):
        rob = rand_xy()
        while (rob in robs):
            rob = rand_xy()
        robs.append(rob)
    dirts = []
    if n_dirt == maxint: num_dirts  = randint(0,x*y-num_robots)
    else: num_dirts = min(n_dirt, x*y-num_robots)
    for i in range(num_dirts):
        dirt = rand_xy()
        while (dirt in dirts or dirt in robs):
            dirt = rand_xy()
        dirts.append(dirt)
    obs = []
    if n_obs == maxint: num_obs  = randint(0,x*y-num_robots-num_dirts)
    else: num_obs = min(n_obs,x*y-num_robots-num_dirts)
    for i in range(num_obs):
        ob = rand_xy()
        while (ob in dirts or ob in robs or ob in obs):
            ob = rand_xy()
        obs.append(ob)
    return robprob.MultiRobotState(x,y,tuple(robs), tuple(dirts), tuple(obs))

def gal_sets(n_sets, set_size):

    problems_set = []
    size = 2
    obs = 0
    dirt = 1
    robs = 1
    for i in range(n_sets):
        problems = []
        for j in range(set_size):
            problems.append( random_problem(size,size, robs, dirt, obs))
        problems_set.append(problems)
        if  (i % 4 == 0): size += 1
        elif(i % 4 == 1): robs += 1
        elif(i % 4 == 2): dirt += 1
        elif(i % 4 == 3): obs  += 1
    return problems_set

def yonatan_sets(n_sets, set_size):
    problems_set = []
    size = 2
    obs = 0
    dirt = 1
    robs = 1
    for i in range(n_sets):
        problems = []
        for j in range(set_size):
            problems.append( random_problem(size,size, robs, dirt, obs))
        problems_set.append(problems)
        obs  += 1
        dirt += 1
        robs += 1
        size += 1
    return problems_set

def incrementing_dirt_sets(n_sets, set_size):
    problems_set = []
    size = 5
    obs = 0
    dirt = 1
    robs = 1
    for i in range(n_sets):
        problems = []
        for j in range(set_size):
            problems.append( random_problem(size,size, robs, dirt, obs))
        problems_set.append(problems)
        
        dirt += 1
        
    return problems_set

def incrementing_obs_sets(n_sets, set_size):
    problems_set = []
    size = 5
    obs = 0
    dirt = 1
    robs = 1
    for i in range(n_sets):
        problems = []
        for j in range(set_size):
            problems.append( random_problem(size,size, robs, dirt, obs))
        problems_set.append(problems)
        
        obs += 1
        
    return problems_set

def incrementing_dirt_obs_sets(n_sets, set_size):
    problems_set = []
    size = 5
    obs = 0
    dirt = 1
    robs = 1
    for i in range(n_sets):
        problems = []
        for j in range(set_size):
            problems.append( random_problem(size,size, robs, dirt, obs))
        problems_set.append(problems)
        
        dirt += 1
        obs  += 1
    return problems_set
    
def parse_problem(problem_):

    problem = problem_.splitlines()[2:]
    problem = problem[:-1]
    y = len(problem)
    x = len(problem[0])-2
    robs = []
    obs  = []
    dirt = []
    
    for i in range(y):
        line = problem[i][1:]
        line = line[:-1]
        for j in range(x):
            position = line[j]
            if position.upper() == 'X': obs.append((j,i))
            if position.isdigit(): robs.append((j,i))
            if position.upper() == '*': dirt.append((j,i)) 
    return robprob.MultiRobotState(x,y,tuple(robs), tuple(dirt), tuple(obs))
"""
if False:
    for p in [graphic_problems.p1, graphic_problems.p2,graphic_problems.p3]:
        print 'this is the raw problem'
        print p
        print 'this is the parsed problem'
        p2 = parse_problem(p)
        print p2
"""
